<?php
	

function dhtmlspecialchars($string) {
	if(is_array($string)) {
		foreach($string as $key => $val) {
			$string[$key] = dhtmlspecialchars($val);
		}
	} else {
		$string = preg_replace( "@<script(.*?)</script>@is", "", $string );
        $string = preg_replace( "@<iframe(.*?)</iframe>@is", "", $string );
        $string = preg_replace( "@<style(.*?)</style>@is", "", $string );
        $string = preg_replace( "@<(.*?)>@is", "", $string );
		$string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
		str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
	}
	return $string;
}

function daddslashes($string, $force = 0) {
	!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
	if(!MAGIC_QUOTES_GPC || $force) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = daddslashes($val, $force);
			}
		} else {
			$string = addslashes($string);
		}
	}
	return $string;
}

function fenpage($listsum,$page_size=2){
    global $prepage,$nextpage,$pages,$sums,$startpos,$eachpage; //out param
    $page = isset($_GET['page']) ? intval($_GET["page"]) : 0;
    $eachpage = $page_size;
    $sums = $listsum;
    $pages = ceil(($sums-0.5)/$eachpage)-1;
    $pages = $pages>=0?$pages:0;
    $prepage = ($page>0)?$page-1:0;
    $nextpage = ($page<$pages)?$page+1:$pages;
    $startpos = $page*$eachpage;
}


function showmassage($massage='',$forward_url=''){
  header("location:".$forward_url);
  exit(); 
}

function isemail($email) {
	return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}

function formhash($specialadd = '') {
	return substr(md5(time()), 8,8);	
}

function arrayeval($array, $level = 0) {
	if(!is_array($array)) {
		return "'".$array."'";
	}
	if(is_array($array) && function_exists('var_export')) {
		return var_export($array, true);
	}

	$space = '';
	for($i = 0; $i <= $level; $i++) {
		$space .= "\t";
	}
	$evaluate = "Array\n$space(\n";
	$comma = $space;
	if(is_array($array)) {
		foreach($array as $key => $val) {
			$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
			$val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
			if(is_array($val)) {
				$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
			} else {
				$evaluate .= "$comma$key => $val";
			}
			$comma = ",\n$space";
		}
	}
	$evaluate .= "\n$space)";
	return $evaluate;
}





function getip(){
    if (isset($_SERVER)) {
        if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) {
            $realip = $_SERVER[HTTP_X_FORWARDED_FOR];
        } elseif (isset($_SERVER[HTTP_CLIENT_IP])) {
            $realip = $_SERVER[HTTP_CLIENT_IP];
        } else {
            $realip = $_SERVER[REMOTE_ADDR];
        }
    } else {
       if (getenv("HTTP_X_FORWARDED_FOR")) {
           $realip = getenv( "HTTP_X_FORWARDED_FOR");
       } elseif (getenv("HTTP_CLIENT_IP")) {
           $realip = getenv("HTTP_CLIENT_IP");
       } else {
           $realip = getenv("REMOTE_ADDR");
       }
    }
    return $realip;
}

//取得用用户信息
function getuserinfo($uid){
	global $db;
	$result = 0;
	$user = $db->getDataList('day_userinfo',array('where' => array('uid' => $uid)));
	
	if(empty($user)){
		return false;
	}else{
		if(empty($user[0]['weightnow']) || empty($user[0]['height'])){
			return false;
		}
	}
	return $user;
}


//计算是摄食
function getCal($age,$grener){
	if($age>=18 && $age<=49){
		if($grener == 1){
			$cal[1] = 2400;
			$cal[2] = 3200;
		}elseif($grener == 2){
			$cal[1] = 2100;
			$cal[2] = 2700;
		}
	}
	if($age>=50 && $age<=59){
		if($grener == 1){
			$cal[1] = 2300;
			$cal[2] = 3100;
		}elseif($grener == 2){
			$cal[1] = 2100;
			$cal[2] = 2700;
		}
	}
	if($age>=60 && $age<=69){
		if($grener == 1){
			$cal[1] = 1900;
			$cal[2] = 2200;
		}elseif($grener == 2){
			$cal[1] = 1800;
			$cal[2] = 2000;
		}
	}
	
	if($age>=70 && $age<=79){
		if($grener == 1){
			$cal[1] = 1900;
			$cal[2] = 2100;
		}elseif($grener == 2){
			$cal[1] = 1700;
			$cal[2] = 1900;
		}
	}
	if($age>=80){
		if($grener == 1){
			$cal[1] = 1900;
			$cal[2] = 1900;
		}elseif($grener == 2){
			$cal[1] = 1700;
			$cal[2] = 1700;
		}
	}
	return $cal;
}

//取得每天饮食卡路里
function getCala($age,$thecal,$grener){
	$h = getCal($age,$grener);
	
	if($thecal > $h[1]){
		if($tecal < $h[2]){
			$result = $thecal - $h[1];
		}else{
			$result = $thecal-$h[2];
		}
	}else{
		$result = $h[1] - $thecal;
	}
	
	$result = $h[2] - $thecal;
	return $result;
}


function getAge($birthday){
	if($birthday){
		$date_1 = date("Y-m-d");
		$date_2 = $birthday;
		if(empty($date_2)){
			exit('age empty');
		}
		$date_explode_1=explode("-",$date_1);	
		$date_explode_2=explode("-",$date_2);
		$day_1=mktime(0,0,0,$date_explode_1[1],$date_explode_1[2],$date_explode_1[0]);
		$day_2=mktime(0,0,0,$date_explode_2[1],$date_explode_2[2],$date_explode_2[0]);
		$days=round(($day_1-$day_2)/3600/24);
	}else{
		$days = 0;
	}

	return $days;	
}

/**
*getConstellation 根据出生生日取得星座
*
String $brithday 用于得到星座的日期 格式为yyyy-mm-dd
*
Array $format 用于返回星座的名称
*
String
*/
function getConstellation($birthday, $format=null)
{
$pattern = '/^\d{4}-\d{1,2}-\d{1,2}$/';
if (!preg_match($pattern, $birthday, $matchs))
{
    return null;
}
$date = explode('-', $birthday);
$year = $date[0];
$month = $date[1];
$day   = $date[2];
if ($month <1 || $month>12 || $day < 1 || $day >31)
{
    return null;
}
//设定星座数组
$constellations = array(
      '摩羯座', '水瓶座', '双鱼座', '白羊座', '金牛座', '双子座',
      '巨蟹座','狮子座', '处女座', '天秤座', '天蝎座', '射手座',);

//设定星座结束日期的数组，用于判断
$enddays = array(19, 18, 20, 20, 20, 21, 22, 22, 22, 22, 21, 21,);
//如果参数format被设置，则返回值采用format提供的数组，否则使用默认的数组
if ($format != null)
{
    $values = $format;
}
else
{
    $values = $constellations;
}
//根据月份和日期判断星座
switch ($month)
{
    case 1:
      if ($day <= $enddays[0])
      {
        $constellation = $values[0];
      }
      else
      {
        $constellation = $values[1];
      }
      break;
    case 2:
      if ($day <= $enddays[1])
      {
        $constellation = $values[1];
      }
      else
      {
        $constellation = $values[2];
      }
      break;
    case 3:
      if ($day <= $enddays[2])
      {
        $constellation = $values[2];
      }
      else
      {
        $constellation = $values[3];
      }
      break;
    case 4:
      if ($day <= $enddays[3])
      {
        $constellation = $values[3];
      }
      else
      {
        $constellation = $values[4];
      }
      break;
    case 5:
      if ($day <= $enddays[4])
      {
        $constellation = $values[4];
      }
      else
      {
        $constellation = $values[5];
      }
      break;
    case 6:
      if ($day <= $enddays[5])
      {
        $constellation = $values[5];
      }
      else
      {
        $constellation = $values[6];
      }
      break;
    case 7:
      if ($day <= $enddays[6])
      {
        $constellation = $values[6];
      }
      else
      {
        $constellation = $values[7];
      }
      break;
    case 8:
      if ($day <= $enddays[7])
      {
        $constellation = $values[7];
      }
      else
      {
        $constellation = $values[8];
      }
      break;
    case 9:
      if ($day <= $enddays[8])
      {
        $constellation = $values[8];
      }
      else
      {
        $constellation = $values[9];
      }
      break;
    case 10:
      if ($day <= $enddays[9])
      {
        $constellation = $values[9];
      }
      else
      {
        $constellation = $values[10];
      }
      break;
    case 11:
      if ($day <= $enddays[10])
      {
        $constellation = $values[10];
      }
      else
      {
        $constellation = $values[11];
      }
      break;
    case 12:
      if ($day <= $enddays[11])
      {
        $constellation = $values[11];
      }
      else
      {
        $constellation = $values[0];
      }
      break;
}
return $constellation;
}
?>
